package com.smartwebx.ha.web.gis.module.screen;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRiverManager;
import com.smartwebx.ha.biz.rainwater.real.RiverFloodControlManager;
import com.smartwebx.ha.biz.rainwater.real.RiverManager;
import com.smartwebx.ha.biz.rainwater.real.RvsectManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.annot.JsonModules;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.FeatureHighLow;
import com.smartwebx.ha.dal.usebean.RiverFC;
import com.smartwebx.ha.dal.usebean.RiverFlood;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.json.module.DoubleModule;

public class RiverScreen extends BasicScreen {
	@Autowired
	private RiverManager riverManager;
	@Autowired
	private RvsectManager rvsectManager;
	@Autowired
	private RiverFloodControlManager riverFloodControlManager;
	@Autowired
	private FeatureRiverManager featureRiverManager;

	/**
	 * 河道站单站小时过程
	 * 
	 * @param stcd
	 *            (50916500)
	 * @param bTm
	 *            (2011-07-11)
	 * @param eTm
	 *            (2011-08-20)
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<ViewProcessBean> getSingleStatHourProcess(String stcd, Date bTm, Date eTm)
			throws IllegalAccessException, InvocationTargetException {
		// List<DateValue> dateValueRivers = riverManager
		// .getPurenessWaterProcesss(stcd, bTm, eTm);// 河道水位小时过程
		List<DateValue> dateValueRivers = riverManager.getSingleStatWaterHourProcess(stcd, bTm, eTm);// 河道水位小时过程
		// List<DateValue> dateValueFlows =
		// riverManager.getPurenessFlowProcesss(
		// stcd, bTm, eTm);// 河道流量小时过程
		List<DateValue> dateValueFlows = riverManager.getSingleStatFlowHourProcess(stcd, bTm, eTm);// 河道流量小时过程

		RiverFC riverFC = riverFloodControlManager.getSingleRiverFloodControl(stcd);
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueRiver : dateValueRivers) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueRiver);
			viewProcessBean.setTotalVal(riverFC == null ? 0 : riverFC.getWarWater());// 警戒水位线
			viewProcessBean.setValue1(riverFC == null ? 0 : riverFC.getSavFlow());// 保证水位
			for (DateValue dateValueFlow : dateValueFlows) {
				if (dateValueFlow.getTm().compareTo(dateValueRiver.getTm()) == 0) {
					viewProcessBean.setValue2(dateValueFlow.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;
	}

	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public Object getSingleStatHourProcessByGisJs(String stcd, Date bTm, Date eTm)
			throws IllegalAccessException, InvocationTargetException {
		// List<DateValue> dateValueRivers = riverManager
		// .getPurenessWaterProcesss(stcd, bTm, eTm);// 河道水位小时过程
		List<DateValue> dateValueRivers = riverManager.getSingleStatWaterHourProcess(stcd, bTm, eTm);// 河道水位小时过程
		// List<DateValue> dateValueFlows =
		// riverManager.getPurenessFlowProcesss(
		// stcd, bTm, eTm);// 河道流量小时过程
		List<DateValue> dateValueFlows = riverManager.getSingleStatFlowHourProcess(stcd, bTm, eTm);// 河道流量小时过程
		

		List<Double> riverVals = Lists.newArrayList();// 水位过程
		List<Double> value2 = Lists.newArrayList();// 流量
		for (DateValue dateValueRiver : dateValueRivers) {
			riverVals.add(dateValueRiver.getVal());

			for (DateValue dateValueFlow : dateValueFlows) {
				if (dateValueFlow.getTm().compareTo(dateValueRiver.getTm()) == 0) {
					value2.add(dateValueFlow.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = ImmutableMap.of("riverVals", riverVals, "value2", value2);
		return result;
	}

	/**
	 * 河道站单站每日8时过程
	 * 
	 * @param stcd
	 *            (50916500) 河道站站码
	 * @param bTm
	 *            (2011-07-11) 开始日期
	 * @param eTm
	 *            (2011-08-20) 结束日期
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm, Date eTm, Integer dxn)
			throws Exception {
		List<DateValue> dateValueWaters = riverManager.getSingleStatWaterDayProcess(stcd, bTm, eTm);
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		// 典型年
		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = riverManager.getSingleStatWaterDayProcess(stcd, startDate, endDate);
		RiverFC riverFC = riverFloodControlManager.getSingleRiverFloodControl(stcd);
		// 多年同期
		List<DateValue> dateValueYears = featureRiverManager.getPeriodContrast(stcd, bTm, eTm);
		FeatureHighLow featureHighLow = featureRiverManager.getMoreYearStatHighLowWater(stcd);
		List<DateValue> dateValueFlows = riverManager.getSingleStatFlowDayProcess(stcd, bTm, eTm);// 流量过程
		List<DateValue> dateValueHDGCs = rvsectManager // 河底高程
				.getSingleStatHDGCDayProcess(stcd, bTm, bTm);
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();

		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		for (DateValue dateValueWater : dateValueWaters) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);
			/*
			 * viewProcessBean.setDate(dateValueWater.getTm());
			 * viewProcessBean.setValue(dateValueWater.getVal());
			 */
			viewProcessBean.setTotalVal(riverFC == null ? 0 : riverFC.getWarWater());// 警戒水位线
			viewProcessBean.setValue4(riverFC == null ? 0 : riverFC.getSavWater());// 保证水位
			viewProcessBean.setValue2(featureHighLow.getMaxz());// 历史最高
			viewProcessBean.setValue3(featureHighLow.getMinz());// 历史最低
			for (DateValue dateValueYear : dateValueYears) {
				if (Objects.equal(dateValueWater.getTm(), DateUtils.addHours(dateValueYear.getTm(), 8))) {
					viewProcessBean.setValue1(dateValueYear.getVal());
					break;
				}
			}
			for (DateValue dateValueFlow : dateValueFlows) {
				if (dateValueFlow.getTm().compareTo(dateValueWater.getTm()) == 0) {
					viewProcessBean.setValue5(dateValueFlow.getVal());
					break;
				}
			}
			for (DateValue dateValueHDGC : dateValueHDGCs) {
				if (dateValueHDGC.getTm().compareTo(dateValueWater.getTm()) == 0) {
					viewProcessBean.setHdgc(dateValueHDGC.getVal());
					break;
				}
			}
			for (DateValue dxnDateValue : dxnDateValues) {
				String s1 = sdf.format(dxnDateValue.getTm());
				String s2 = sdf.format(dateValueWater.getTm());
				if (s1.equals(s2)) {
					viewProcessBean.setValue6(dxnDateValue.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;
	}

	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm, Date eTm, Integer dxn) throws Exception {
		List<DateValue> dateValueWaters = riverManager.getSingleStatWaterDayProcess(stcd, bTm, eTm);
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		// 典型年
		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = riverManager.getSingleStatWaterDayProcess(stcd, startDate, endDate);
		RiverFC riverFC = riverFloodControlManager.getSingleRiverFloodControl(stcd);
		// 多年同期
		List<DateValue> dateValueYears = featureRiverManager.getPeriodContrast(stcd, bTm, eTm);
		FeatureHighLow featureHighLow = featureRiverManager.getMoreYearStatHighLowWater(stcd);
		List<DateValue> dateValueFlows = riverManager.getSingleStatFlowDayProcess(stcd, bTm, eTm);// 流量过程
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<Double> vals = Lists.newArrayList();// 日水位过程
		List<Double> totalVals = Lists.newArrayList();// 警戒水位
		List<Double> values4 = Lists.newArrayList();// 保证水位
		List<Double> values1 = Lists.newArrayList();// 多年同期
		List<Double> values5 = Lists.newArrayList();// 流量过程
		List<Double> values6 = Lists.newArrayList();// 典型年
		List<Double> values2 = Lists.newArrayList();// 历史最高
		List<Double> values3 = Lists.newArrayList();// 历史最低
		for (DateValue dateValueWater : dateValueWaters) {

			vals.add(dateValueWater.getVal());
			totalVals.add(riverFC == null ? 0 : riverFC.getWarWater());// 警戒水位线
			values4.add(riverFC == null ? 0 : riverFC.getSavWater());// 保证水位
			values2.add(featureHighLow.getMaxz());// 历史最高
			values3.add(featureHighLow.getMinz());// 历史最低
			for (DateValue dateValueYear : dateValueYears) {
				if (Objects.equal(dateValueWater.getTm(), DateUtils.addHours(dateValueYear.getTm(), 8))) {
					values1.add(dateValueYear.getVal());
					break;
				}
			}
			for (DateValue dateValueFlow : dateValueFlows) {
				if (dateValueFlow.getTm().compareTo(dateValueWater.getTm()) == 0) {
					values5.add(dateValueFlow.getVal());
					break;
				}
			}
			for (DateValue dxnDateValue : dxnDateValues) {
				String s1 = sdf.format(dxnDateValue.getTm());
				String s2 = sdf.format(dateValueWater.getTm());
				if (s1.equals(s2)) {
					values6.add(dxnDateValue.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("riverVals", vals);
		result.put("riverTotVals", totalVals);
		result.put("values4", values4);
		result.put("values5", values5);
		result.put("values6", values6);
		result.put("values1", values1);
		return result;
	}

	/**
	 * 获取河道防洪指标信息
	 * 
	 * @param stcd
	 *            (50916500)
	 * @return
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public RiverFlood getRVFCCH(String stcd) {
		RiverFlood riverFlood = riverManager.getRiverFlood(stcd);
		if (riverFlood == null) {
			return new RiverFlood();
		} else {
			return riverFlood;
		}
	}

	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "2") })
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd, Date bTm, Date eTm) {
		ViewDayProcessAnalysis viewDayProcessAnalysis = riverManager.getDayProcessAnalysis(stcd, bTm, eTm);
		return viewDayProcessAnalysis;
	}

}
